package cn.frank.framework.caesar.module.ai.common.util;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @description: TODO
 * @author: renjiangyang
 * @date 2025/6/8 19:30:05
 * @since 1.8
 */
public class AILinkParserHelper {

    @Data
    public static class AIModelInfo {
        private final String url;
        private final String key;
        private final String model;

        public AIModelInfo(String url, String key, String model) {
            this.url = url;
            this.key = key;
            this.model = model;
        }

        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }

    /**
     * 解析 ai:// 开头的连接字符串，格式：
     * ai://host:port/path!key!model-name
     */
    public static AIModelInfo parse(String input) {
        if (input == null || !input.startsWith("ai://")) {
            throw new IllegalArgumentException("输入必须以 'ai://' 开头");
        }

        // 去掉 ai:// 前缀
        String content = input.substring("ai://".length());

        // 分割三部分：host_part + !key! + model
        int firstExclamation = content.indexOf('!');
        if (firstExclamation == -1) {
            throw new IllegalArgumentException("缺少第一个 '!' 分隔符");
        }

        int secondExclamation = content.indexOf('!', firstExclamation + 1);
        if (secondExclamation == -1) {
            throw new IllegalArgumentException("缺少第二个 '!' 分隔符");
        }

        String hostPart = content.substring(0, firstExclamation);
        String key = content.substring(firstExclamation + 1, secondExclamation);
        String model = content.substring(secondExclamation + 1);

        // 转换 hostPart 为 http 格式
        String fullUrl = "http://" + hostPart;

        return new AIModelInfo(fullUrl, key, model);
    }

    // 测试方法
    public static void main(String[] args) {
        String input = "ai://10.200.82.19:8000/v1!123!Valdemardi/DeepSeek-R1-Distill-Qwen-32B-AWQ";
        AIModelInfo info = parse(input);
        System.out.println(info.toString());
    }


}
